/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.entity;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.core.tenant.mp.TenantEntity;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.enums.ApproveStatusEnum;
import org.springblade.modules.repair.enums.MaintainPlanContentEnum;
import org.springblade.modules.repair.enums.TaskStatusEnum;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 维保任务表实体类
 *
 * @author ysq
 * @since 2022-03-28
 */
@Data
@TableName("blade_maintain_plan_content")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "MaintainPlanContent对象", description = "维保任务表")
public class MaintainPlanContent extends TenantEntity {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "流程定义主键")
	private String processDefinitionId;

	@ApiModelProperty(value = "流程实例主键")
	private String processInstanceId;

	/**
	 * 电梯id
	 */
	@ApiModelProperty(value = "电梯id")
	private Long elevatorId;
	/**
	 * 1: 保养, 2: 维修, 3: 困人
	 */
	@ApiModelProperty(value = "1: 保养, 2: 维修, 3: 困人")
	private Integer typeStatus;
	/**
	 * 开始时间
	 */
	@ApiModelProperty(value = "开始时间")
	private Date planDate;
	/**
	 * 结束时间
	 */
	@ApiModelProperty(value = "结束时间")
	private Date finishDate;
	/**
	 * 0: 未延迟, 1: 延迟
	 */
	@ApiModelProperty(value = "0: 未延迟, 1: 延迟")
	private Integer isDeferred;
	/**
	 * 任务来源: 1: 物业端, 2: 微信, 3: 服务电话, 4: 系统派单, 5: 维保端
	 */
	@ApiModelProperty(value = "任务来源: 1: 物业端, 2: 微信, 3: 服务电话, 4: 系统派单, 5: 维保端")
	private Integer planFrom;




	/**
	 * 提交地址
	 */
	@TableField(exist = false)
	private String commitAddress;

	/**
	 * 提交经度
	 */
	@TableField(exist = false)
	private String commitLongitude;

	/**
	 * 提交纬度
	 */
	@TableField(exist = false)
	private String commitLatitude;


	/**
	 * 提交纬度
	 */
	@TableField(exist = false)
	@ApiModelProperty("维修类型1:撤回，2:关闭,3已驳回")
	private Integer wxTypeStatus;

	/**
	 * 关联人员id
	 */
	@TableField(exist = false)
	private Long userId;

	/**
	 * 关联人员名称
	 */
	@TableField(exist = false)
	private String userName;


	@TableField(exist = false)
	@ApiModelProperty("保养工单名称")
	private String planCheckName;

	/**
	 * 预计完成时间
	 */
	private Date planFinishDate;

	/**
	 * 来源工单类型
	 */
	private Integer sourcePlanType;

	@TableField(exist = false)
	private String planStatusName;

	@TableField(exist = false)
	private String typeStatusName;

	@TableField(exist = false)
	private String partsModel;

	@TableField(exist = false)
	private Integer auditStatus;

	@TableField(exist = false)
	private String auditStatusName;

	@TableField(exist = false)
	private Long malWorksId;

	public void setPlanCheckName(String planCheckName) {
		if(Func.isNotEmpty(planCheckName) && planCheckName.contains("%")){
			List<String> strings = Func.toStrList(planCheckName);
			List<MaintainPlanTemplate> list = new ArrayList<>(10);
			for (String str :strings) {
				String[] split = str.split("%");
				MaintainPlanTemplate maintainPlanTemplate = new MaintainPlanTemplate();
				maintainPlanTemplate.setTemplateName(split[1]);
				maintainPlanTemplate.setSort(Convert.toInt(split[2],0));
				list.add(maintainPlanTemplate);
			}
			List<String> collect = list.stream().sorted(Comparator.comparing(MaintainPlanTemplate::getSort)).map(MaintainPlanTemplate::getTemplateName).collect(Collectors.toList());
			this.planCheckName = String.join(",",collect);
		}else{
			this.planCheckName = planCheckName;
		}
	}

	public void setAuditStatus(Integer auditStatus) {
		this.auditStatus = auditStatus;
		if (Func.isNotEmpty(auditStatus)) {
			this.auditStatusName = ApproveStatusEnum.getName(auditStatus);
		}
	}

	public void setTypeStatus(Integer typeStatus) {
		this.typeStatus = typeStatus;
		if (Func.isNotEmpty(typeStatus)) {
			this.typeStatusName = MaintainPlanContentEnum.getName(typeStatus);
		}
	}

	public String getPlanStatusName() {
		if (Func.isNotEmpty(this.getTypeStatus()) && Func.isNotEmpty(this.getStatus())) {
			return TaskStatusEnum.getTaskName(this.getTypeStatus(), this.getStatus());
		}
		return null;
	}
}
